
package view;

import domain.DropboxController;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.net.ConnectException;
import java.util.List;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/**
 *
 * @author Joachim
 */
public class DropboxFrame extends JFrame {
    
    private DropboxController controller;
    private String rootDirectory;
    private JTextField txtRootDirectory;

    public DropboxFrame(DropboxController controller) {
        
        this.controller = controller;
        this.rootDirectory = controller.getRootDirectory();
        
        this.setSize(500, 200);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        this.setLayout(new GridBagLayout());
 
        GridBagConstraints c = new GridBagConstraints();
        c.fill = GridBagConstraints.BOTH;
        c.insets =  new Insets(10,10,10,10);
        
        {
            txtRootDirectory = new JTextField(35);
            txtRootDirectory.setEditable(false);
            txtRootDirectory.setText(rootDirectory);
            c.gridx = 0;
            c.gridy = 0;
            this.add(txtRootDirectory,c);
        }
        
        {
            JButton btnSelectRootDirectory = new JButton("...");
            btnSelectRootDirectory.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    selectRootDirectory();
                }
            });
            c.gridx = 1;
            c.gridy = 0;
            this.add(btnSelectRootDirectory,c);
        }
        
        /*
        {
            JButton btnSave = new JButton("Save settings");
            btnSave.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    saveSettings();
                }
            });
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 2;
            this.add(btnSave, c);
        }
        */
        
        /*
        {
            JButton btnSynchronize = new JButton("Synchronize");
            btnSynchronize.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    synchronize();
                }
            });
            c.gridx = 0;
            c.gridy = 2;
            c.gridwidth = 2;
            this.add(btnSynchronize, c);
        }
        */

        {
            JCheckBox checkListenDirectory = new JCheckBox("Synchronize");
            checkListenDirectory.addItemListener(new ItemListener() {
                @Override
                public void itemStateChanged(ItemEvent e) {
                    if(e.getStateChange() == ItemEvent.SELECTED)
                        setSynchronized(true);
                    else if(e.getStateChange() == ItemEvent.DESELECTED)
                        setSynchronized(false);
                }
            });
            c.gridx = 0;
            c.gridy = 1;
            c.gridwidth = 2;
            this.add(checkListenDirectory, c);
        }

        // Taskbar icons
        List<Image> icons = new ArrayList<Image>();
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo32x32.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo64x64.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo128x128.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo256x256.png")).getImage());
        icons.add(new ImageIcon(getClass().getResource("../resources/logos/logo512x512.png")).getImage());
        this.setIconImages(icons);
    }
    
    private void selectRootDirectory() {
        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int fileChooserResult = fileChooser.showOpenDialog(this);
        if(fileChooserResult == JFileChooser.APPROVE_OPTION) {
            File folderName = fileChooser.getSelectedFile();
            rootDirectory = folderName.getPath();
            controller.setRootDirectory(rootDirectory);
            txtRootDirectory.setText(rootDirectory);
        }
    }
    
    /*
    private void saveSettings() {
        try {
            System.out.println(rootDirectory);
            controller.setRootDirectory(rootDirectory);
        }
        catch(InvalidPathException ipe) {
            JOptionPane.showMessageDialog(null, ipe.getMessage());
        }
    }
    */
    
    private void setSynchronized(boolean synched) {
        try {System.out.println("synchring" + synched);
            controller.setDirectoryListener(synched);
            if(synched)
                controller.synchronize();
        } catch (ConnectException ce) {
            JOptionPane.showMessageDialog(null, ce.getMessage());
        }
    }
   
}
